import { UserService } from './user.service';
// import { CreateUserDto } from './dto/create-user.dto';
// import { UpdateUserDto } from './dto/update-user.dto';

import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject } from '@nestjs/common';
import { Controller, Get, Post, Body } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Controller('user')
export class UserController {
  constructor(
    @Inject(CACHE_MANAGER) private cacheManager: Cache,
    private readonly userService: UserService,
  ) {}

  @Get('/clienthome')
  getClientHome() {
    return this.userService.getClientHome();
  }

  @Post('/clienthome')
  createClientHome(@Body() body) {
    return this.userService.createClientHome(body);
  }

  @Post('/logo')
  createLogo(@Body() body) {
    return this.userService.createLogo(body);
  }

  @Get('/logo')
  getLogo() {
    return this.userService.getLogo();
  }

  @Post('/register')
  async createUser(@Body() body) {
    //获取缓存验证码
    const codeCache = await this.cacheManager.get('key');
    console.log(codeCache);
    body.codeCache = codeCache;
    await this.cacheManager.del('key');
    return this.userService.createUser(body);
  }

  @Post('/login')
  async login(@Body() body) {
    return this.userService.login(body);
  }

  @Post('/email')
  async sendEmail(@Body() body) {
    const { email } = body;

    const code = Math.random().toString().slice(-6);
    //缓存验证码
    await this.cacheManager.set('key', code, 0);
    // const codeCache = await this.cacheManager.get('key');

    return this.userService.toEmail(email, code);
  }

  //自动登录
  @Post('/autoLogin')
  async autoLogin(@Body() body) {
    return this.userService.autoLogin(body);
  }

  //更新用户信息
  @Post('/update')
  async updateUser(@Body() body) {
    return this.userService.updateUser(body);
  }

  //获取用户数量
  @Get('/count')
  async getUserCount() {
    return this.userService.getUserCount();
  }

  
}
